<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon-32x32-st.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-st.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-32x32-st.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="读书笔记,《JavaScript 高级程序设计》,">





  <link rel="alternate" href="/atom.xml" title="卷柏的花期" type="application/atom+xml">






<meta name="description" content="本篇的重点在于理解正则表达式的组成部分，掌握正则实例对象的属性、方法以及正则构造函数的静态属性。">
<meta name="keywords" content="读书笔记,《JavaScript 高级程序设计》">
<meta property="og:type" content="article">
<meta property="og:title" content="《JavaScript 高级程序设计》第五章：RegExp">
<meta property="og:url" content="http://yoursite.com/2019/04/11/professional-javascript-chapter-5-RegExp/index.html">
<meta property="og:site_name" content="卷柏的花期">
<meta property="og:description" content="本篇的重点在于理解正则表达式的组成部分，掌握正则实例对象的属性、方法以及正则构造函数的静态属性。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://yoursite.com/2019/04/11/professional-javascript-chapter-5-RegExp/javascript.jpg">
<meta property="og:updated_time" content="2019-04-10T16:11:50.871Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="《JavaScript 高级程序设计》第五章：RegExp">
<meta name="twitter:description" content="本篇的重点在于理解正则表达式的组成部分，掌握正则实例对象的属性、方法以及正则构造函数的静态属性。">
<meta name="twitter:image" content="http://yoursite.com/2019/04/11/professional-javascript-chapter-5-RegExp/javascript.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/04/11/professional-javascript-chapter-5-RegExp/">





  <title>《JavaScript 高级程序设计》第五章：RegExp | 卷柏的花期</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband">
    <a href="https://gitee.com/Selaginella/" target="_blank" style="position:absolute;right:0;top:0;border:none"><img src="https://gitee.com/Selaginella/Selaginella/widgets/widget_1.svg" alt="Fork me on Gitee"></a>
    </div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">卷柏的花期</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">随风而动，九死还生</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-source">
          <a href="/source/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-flask"></i> <br>
            
            资源
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/04/11/professional-javascript-chapter-5-RegExp/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Selaginella">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="卷柏的花期">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">《JavaScript 高级程序设计》第五章：RegExp</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-04-11T07:47:15+08:00">
                2019-04-10
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/JavaScript/" itemprop="url" rel="index">
                    <span itemprop="name">JavaScript</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote class="blockquote-center"><img src="/2019/04/11/professional-javascript-chapter-5-RegExp/javascript.jpg" alt="&#39;JavaScript 高级程序设计&#39;">本篇的重点在于理解正则表达式的组成部分，掌握正则实例对象的属性、方法以及正则构造函数的静态属性。</blockquote>

<a id="more"></a>
<h2 id="创建-RegExp-表达式"><a href="#创建-RegExp-表达式" class="headerlink" title="创建 RegExp 表达式"></a>创建 RegExp 表达式</h2><p>在 ECMAScript 中创建正则表达式的方式有两种：</p>
<ul>
<li>字面量</li>
<li>RegExp构造函数</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> expression = <span class="regexp">/pattern/m</span>odifier</span><br><span class="line"><span class="keyword">var</span> expression = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"pattern"</span>,<span class="string">"modifier"</span>);</span><br></pre></td></tr></table></figure>
<p>一个完整的正则表达式主要有两部分组成：“匹配模式(pattern)”与“修饰符(modifier)”。<br>“匹配模式”是正则表达式的主体”，它定义了具体的匹配规则，而“修饰符”则用于更进一步说明规则的范围和细节：</p>
<ul>
<li><code>g</code> 表示全局匹配。</li>
<li><code>i</code> 表示区分大小写。</li>
<li><code>m</code> 表示多行匹配。</li>
</ul>
<p>再详细一些，“匹配模式”本身又由“普通字符”与“元字符”两部分组成。<br>“普通字符”：我们可以理解为计算机语言中的常量字符，它包括可见字符，比如 a-zA-Z,0-9这些与另一些不可见字符，例如，ASCII 码值在 0-31 的“控制字符”，虽然此类字符不可显示打印，但确实存在，它们通常用于特殊功能的代理。<br>“元字符”：则类似于我们接触到的“变量”，它们在正则表达式中具有特殊的含义与功能，例如字符类、限定符、分组、向前查找以及反向引用等。常见的元字符有：<code>( [ { \ ^ $ | ) ? * + .]}</code>。可以说正则表达式强大的字符匹配功能就是通过对这些“元字符”的按需组合使用。</p>
<p>但是如果我们要匹配的内容中本身就含有元字符，那么在匹配的时候必须要对元字符进行转义，而这个转义功能则由另一个元字符 <code>\</code> 实现。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">\\b =&gt; <span class="string">'\b'</span></span><br><span class="line">\[ =&gt; <span class="string">'['</span></span><br><span class="line">\. =&gt; <span class="string">'.'</span></span><br></pre></td></tr></table></figure>
<p>如果是通过构造函数实例化的正则表达式对象，由于匹配模式与修饰字符都是字符串形式的参数，所以在转义的时候，要进行双重转义。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"\\[abc\\]"</span>,<span class="string">"ig"</span>);</span><br></pre></td></tr></table></figure>
<p>这是因为在字符串中 <code>\</code> 反斜杠本身就具有转义功能，所以如果仍然按照字面量那种单层转义，得到的仍然是元字符本身。例如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&quot;\.&quot; =&gt; &quot;.&quot; =&gt; new RegExp(&quot;\.&quot;) =&gt; /./</span><br></pre></td></tr></table></figure>
<p>这些都是很简单易懂的，稍微有点麻烦的就是要转义本身就含有斜杠符号的元字符，例如 <code>\b</code>、<code>\d</code> 等。对于这样的特殊符号，我们先从转义 斜杠 <code>\</code> 本身开始理解。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/\\/ =&gt; <span class="string">"\\"</span></span><br></pre></td></tr></table></figure>
<p>要注意的是斜杠 <code>\</code> 在字符串中本身就具有转义功能，所以不可能单独存在 “\”。否则会抛出语法异常的，那么下面 <code>\b</code>、<code>\d</code> 就很好理解了。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">/\\b/ =&gt; <span class="string">"\\b"</span> (我们形象上认为的 <span class="string">"\b"</span>形式)。</span><br><span class="line">/\\d/ =&gt; <span class="string">"\\d"</span>  (我们形象上认为的 <span class="string">"\d"</span>形式)。</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"\\\b"</span>) =&gt; <span class="regexp">/\\b/</span></span><br><span class="line"><span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"\\\d"</span>) =&gt; <span class="regexp">/\\d/</span></span><br></pre></td></tr></table></figure>
<h2 id="实例属性"><a href="#实例属性" class="headerlink" title="实例属性"></a>实例属性</h2><p>RegExp 实例对象（字面量对象）所具有的属性。</p>
<ul>
<li>global：布尔值，表示是否设置了 g 标志。</li>
<li>ignoreCase：布尔值，表示是否设置了 i 标志。</li>
<li>multiline：布尔值，表示是否设置了 m 标志。</li>
<li>lastIndex：数值，下次匹配的起始位置，默认值为0.</li>
<li>source：字符串，以字符串的形式返回匹配模式本身。</li>
</ul>
<h2 id="转换方法"><a href="#转换方法" class="headerlink" title="转换方法"></a>转换方法</h2><p>RegExp 实例对象（字面量对象）从 <code>RegExp</code> 的原型上所继承的一些转换方法。<br>与大多数其它引用对象一样，RegExp 类型也重写了 toString，toLocaleSting，valueOf等方法，使他们的功能与返回值更切合自身。<br>在RegExp类型中，<code>toString()</code> 与 <code>toLocaleString()</code> 方法会以字符串的形式返回正则实例对象的表达式，而 <code>valueOf()</code> 则会返回正则表达式本身，即一个对象的类型。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/cat/gi</span>;</span><br><span class="line"></span><br><span class="line">pattern.toString(); <span class="comment">// '/cat/gi' | string</span></span><br><span class="line">pattern.toLocaleString(); <span class="comment">// '/cat/gi' | string</span></span><br><span class="line">pattern.valueOf() <span class="comment">// /cat/gi | object</span></span><br></pre></td></tr></table></figure>
<h2 id="实例方法"><a href="#实例方法" class="headerlink" title="实例方法"></a>实例方法</h2><p>RegExp 实例对象（字面量对象）从 <code>RegExp</code> 的原型上所继承的方法。</p>
<h3 id="test"><a href="#test" class="headerlink" title="test"></a>test</h3><p>功能：根据“匹配模式”在要匹配的字符串中是否具有符合匹配条件的结果，来返回一个布尔值。<br>返回值：Boolean<br>格式：<code>pattern.test(text);</code><br>说明：如果正则表达式中含有全局(g)的修饰符，并进行了多次匹配操作，则 <code>test</code> 会根据 <code>lastIndex</code> 的值作为锚点继续对要匹配的字符串按位置索引继续向下查找匹配。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/a/g</span>;</span><br><span class="line"><span class="keyword">var</span> text = <span class="string">'ababab'</span>;</span><br><span class="line"></span><br><span class="line">pattern.test(text);</span><br><span class="line"><span class="built_in">console</span>.log(pattern.lastIndex);</span><br><span class="line">pattern.test(text);</span><br><span class="line"><span class="built_in">console</span>.log(pattern.lastIndex);</span><br></pre></td></tr></table></figure>
<p>当然 test 常常结合 if 语句进行条件是否成立的判断，所以只需要判断一次即可。</p>
<h3 id="exec"><a href="#exec" class="headerlink" title="exec"></a>exec</h3><p>功能：根据正则表达式的匹配结果来返回一个 Like Array 或者是 null 的结果。<br>返回值：Array | null<br>格式：<code>pattern.exec(text);</code><br>说明：如果表达式在所要匹配的字符串中有符合条件的匹配结果，则返回一个 Link Array，这个 Array 的结构如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[result,group1,group2,...,<span class="attr">input</span>:text,<span class="attr">index</span>:number]</span><br></pre></td></tr></table></figure>
<ul>
<li>result:匹配的结果字符串。</li>
<li>group1，group2…：如果有分组匹配的话，则表示每个分组的值。</li>
<li>input:返回原字符串</li>
<li>index:返回匹配结果首字符在原字符串中的索引位置。</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> text = <span class="string">'ababab'</span>;</span><br><span class="line"><span class="keyword">var</span> pattern1 = <span class="regexp">/(a)/</span>;</span><br><span class="line"><span class="keyword">var</span> pattern2 = <span class="regexp">/.(b)/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(pattern1.exec(text)) <span class="comment">//(2) ["a", "a", index: 0, input: "ababab"]</span></span><br><span class="line"><span class="built_in">console</span>.log(pattern2.exec(text)) <span class="comment">//(2) ["ab", "b", index: 0, input: "ababab"]</span></span><br></pre></td></tr></table></figure>
<p>与 <code>test</code> 方法相同在非全局模式下，一旦匹配到符合规则的结果，便会停止执行，就算是多次执行，返回都是第一次匹配的结果。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> text = <span class="string">'ababab'</span>;</span><br><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/a/g</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(pattern.exec(text))  <span class="comment">//["a", index: 0, input: "ababab", groups: undefined]</span></span><br><span class="line"><span class="built_in">console</span>.log(pattern.exec(text))  <span class="comment">//["a", index: 0, input: "ababab", groups: undefined]</span></span><br><span class="line"><span class="built_in">console</span>.log(pattern.exec(text))  <span class="comment">//["a", index: 0, input: "ababab", groups: undefined]</span></span><br></pre></td></tr></table></figure>
<p>若指定了全局修饰符，那么则会根据正则对象实例的　<code>lastIndex</code> 属性作为锚点作为起点继续对原字符串进行向下匹配。此时每次的匹配结果都会不同。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> text = <span class="string">'ababab'</span>;</span><br><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/a/g</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(pattern.exec(text))  <span class="comment">//["a", index: 0, input: "ababab", groups: undefined]</span></span><br><span class="line"><span class="built_in">console</span>.log(pattern.exec(text))  <span class="comment">//["a", index: 2, input: "ababab", groups: undefined]</span></span><br><span class="line"><span class="built_in">console</span>.log(pattern.exec(text))  <span class="comment">//["a", index: 4, input: "ababab", groups: undefined]</span></span><br><span class="line"><span class="built_in">console</span>.log(pattern.exec(text))  <span class="comment">//null</span></span><br><span class="line"><span class="built_in">console</span>.log(pattern.exec(text))  <span class="comment">//["a", index: 0, input: "ababab", groups: undefined]</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>当匹配到字符串的末尾并返回 <code>null</code> 的时候，则会重新进行匹配。</p>
</blockquote>
<p>通过一个循环收集每个匹配结果的索引位置来深入了解全局模式下的 <code>exec</code>方法。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> text = <span class="string">'ababab'</span>;</span><br><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/a/g</span>;</span><br><span class="line"><span class="keyword">var</span> indexs = [];</span><br><span class="line"><span class="keyword">var</span> result ;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(result = pattern.exec(text))&#123;</span><br><span class="line">    indexs.push(&#123;<span class="attr">index</span>:result.index, <span class="attr">lastIndex</span>:pattern.lastIndex &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="compile"><a href="#compile" class="headerlink" title="compile"></a>compile</h3><p>功能：对原有的正则对象实例重新编译，包括重置匹配模式与修饰符。<br>返回值：正则实例对象本身。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/a/g</span>;</span><br><span class="line">pattern.compile(<span class="string">"b"</span>,<span class="string">"i"</span>);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>说明：一般来说该方法基本不常用，通过对正则实例对象重新赋值也可以达到同等效果。</p>
</blockquote>
<h2 id="RegExp静态属性"><a href="#RegExp静态属性" class="headerlink" title="RegExp静态属性"></a>RegExp静态属性</h2><p>这些属性都是 <code>RegExp</code> 构造函数的静态属性，它们的值受当前作用域正在应用的正则实例对象所影响。</p>
<h3 id="input"><a href="#input" class="headerlink" title="input"></a>input</h3><p>作用域中当前正则对象实例所要匹配的原字符串。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> text = <span class="string">"this has been a short summer"</span>;</span><br><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/(.)hort/g</span>;</span><br><span class="line"></span><br><span class="line">pattern.test(text);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">RegExp</span>.input); <span class="comment">//this has been a short summer</span></span><br></pre></td></tr></table></figure>
<h3 id="lastMatch"><a href="#lastMatch" class="headerlink" title="lastMatch"></a>lastMatch</h3><p>作用域中当前正则实例最近一次匹配结果。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> text = <span class="string">"this has been a short summer"</span>;</span><br><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/(.)hort/g</span>;</span><br><span class="line"></span><br><span class="line">pattern.test(text);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">RegExp</span>.lastMatch); <span class="comment">// short</span></span><br></pre></td></tr></table></figure>
<h3 id="lastParen"><a href="#lastParen" class="headerlink" title="lastParen"></a>lastParen</h3><p>作用域中当前正则实例对象最近一次分组的匹配结果。<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> text = <span class="string">"this has been a short summer"</span>;</span><br><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/(.)hort/g</span>;</span><br><span class="line"></span><br><span class="line">pattern.test(text);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">RegExp</span>.lastParen); <span class="comment">// s</span></span><br></pre></td></tr></table></figure></p>
<h3 id="leftContext"><a href="#leftContext" class="headerlink" title="leftContext"></a>leftContext</h3><p>以作用域中当前正则实例对象的匹配结果为上下文，返回之前的子串</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> text = <span class="string">"this has been a short summer"</span>;</span><br><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/(.)hort/g</span>;</span><br><span class="line"></span><br><span class="line">pattern.exec(text);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">RegExp</span>.leftContext); <span class="comment">//this has been a</span></span><br></pre></td></tr></table></figure>
<h3 id="rightContext"><a href="#rightContext" class="headerlink" title="rightContext"></a>rightContext</h3><p>以作用域中当前正则实例对象的匹配结果为上下文，返回之后的子串<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> text = <span class="string">"this has been a short summer"</span>;</span><br><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/(.)hort/g</span>;</span><br><span class="line"></span><br><span class="line">pattern.exec(text);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">RegExp</span>.rightContext); <span class="comment">// summer</span></span><br></pre></td></tr></table></figure></p>
<h3 id="1-9"><a href="#1-9" class="headerlink" title="$1~$9"></a>$1~$9</h3><p>返回作用域中当前正则实例对象最近一次匹配的1-9个分组结果。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> text =<span class="string">'chart 100'</span>;</span><br><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/(\w+) (\d+)/</span>;</span><br><span class="line">pattern.test(text);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">RegExp</span>.$<span class="number">1</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">RegExp</span>.$<span class="number">2</span>)</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wx.jpg" alt="Selaginella 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="Selaginella 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/读书笔记/" rel="tag"># 读书笔记</a>
          
            <a href="/tags/《JavaScript-高级程序设计》/" rel="tag"># 《JavaScript 高级程序设计》</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/04/02/professional-javascript-chapter-5-Function/" rel="next" title="《JavaScript 高级程序设计》第五章：Function">
                <i class="fa fa-chevron-left"></i> 《JavaScript 高级程序设计》第五章：Function
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/04/13/professional-javascript-chapter-5-Date/" rel="prev" title="《JavaScript 高级程序设计》第五章：Date">
                《JavaScript 高级程序设计》第五章：Date <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC80MTkxNC8xODQ2MA"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/uploads/avatar.jpg" alt="Selaginella">
            
              <p class="site-author-name" itemprop="name">Selaginella</p>
              <p class="site-description motion-element" itemprop="description">知识在于积累，努力必有收获</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">5</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/shenguotao2015" target="_blank" title="gitHub">
                      
                        <i class="fa fa-fw fa-github"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/Selaginella" target="_blank" title="码云">
                      
                        <i class="fa fa-fw fa-telegram"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="http://www.cnblogs.com/HCJJ/" target="_blank" title="博客园">
                      
                        <i class="fa fa-fw fa-rss-square"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:sgt_ah@163.com" target="_blank" title="Email">
                      
                        <i class="fa fa-fw fa-envelope"></i></a>
                  </span>
                
            </div>
          

          
          

          
          

          
        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#创建-RegExp-表达式"><span class="nav-number">1.</span> <span class="nav-text">创建 RegExp 表达式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实例属性"><span class="nav-number">2.</span> <span class="nav-text">实例属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#转换方法"><span class="nav-number">3.</span> <span class="nav-text">转换方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实例方法"><span class="nav-number">4.</span> <span class="nav-text">实例方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#test"><span class="nav-number">4.1.</span> <span class="nav-text">test</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#exec"><span class="nav-number">4.2.</span> <span class="nav-text">exec</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#compile"><span class="nav-number">4.3.</span> <span class="nav-text">compile</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#RegExp静态属性"><span class="nav-number">5.</span> <span class="nav-text">RegExp静态属性</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#input"><span class="nav-number">5.1.</span> <span class="nav-text">input</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#lastMatch"><span class="nav-number">5.2.</span> <span class="nav-text">lastMatch</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#lastParen"><span class="nav-number">5.3.</span> <span class="nav-text">lastParen</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#leftContext"><span class="nav-number">5.4.</span> <span class="nav-text">leftContext</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#rightContext"><span class="nav-number">5.5.</span> <span class="nav-text">rightContext</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-9"><span class="nav-number">5.6.</span> <span class="nav-text">$1~$9</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 &mdash; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-snowflake-o"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Selaginella<span class="with-love"><i class="fa fa-heart"></i></span>Hexo 强力驱动</span>

  
</div>









        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>




















  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
